package com.cn.transfer.jobhandler;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.Product;
import com.cn.transfer.common.entity.jiandaoyun.JdyPurchaseOrder;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.common.utils.Translator;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2023/11/9 14:18
 * @Description: 产品报价翻译字段
 */
@Component
@Slf4j
public class ProductQuotationXxlJob {

    @XxlJob("ProductQuotation")
    public void purchase(){

        String data_id="";
        boolean fale = true;
        do {
            Map<String, Object> parameter = new HashMap<>();
            parameter.put("app_id", "64279a9442e2750008a7b0c1");
            parameter.put("entry_id", "64df0e0300d5ef000806a041");
            parameter.put("data_id", data_id);
            parameter.put("limit", 100);
            String jsonSale = JSONUtil.toJsonStr(parameter);
            Header[] headers = ConnmeUtils.createHeaders();
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/list", headers, jsonSale);
            String data = jdySale.getStr("data");
            JSONArray objects = JSONUtil.parseArray(data);
            List<Product> products = JSONUtil.toList(objects, Product.class);
            if (products.size()==100){
                data_id = products.get(products.size() - 1).get_id();
            }else {
                fale=false;
            }
            try {
                for (Product product : products) {
                    Map<String, Object> parameterInster = new HashMap<>();
                    parameterInster.put("app_id", "64279a9442e2750008a7b0c1");
                    parameterInster.put("entry_id", "64df0e0300d5ef000806a041");
                    parameterInster.put("data_id", product.get_id());
                    Map<String,Object> dataPs =new HashMap<>();
                    if (!product.getProduct_name().equals("")){
                        Map<String,Object> product_name_alt =new HashMap<>();
                        product_name_alt.put("value",Translator.translate("zh-CN", "en", product.getProduct_name()));
                        dataPs.put("product_name_alt",product_name_alt);
                    }
                    if (!product.getSpecification().equals("")){
                        Map<String,Object> specification_alt =new HashMap<>();
                        specification_alt.put("value",Translator.translate("zh-CN", "en", product.getSpecification()));
                        dataPs.put("specification_alt",specification_alt);
                    }

                    if (! product.getMaterial().equals("")){
                        Map<String,Object> material_quality =new HashMap<>();
                        material_quality.put("value",Translator.translate("zh-CN", "en", product.getMaterial()));
                        dataPs.put("material_quality",material_quality);
                    }

                    if (! product.getColor_surface_treatment().equals("")){
                        Map<String,Object> color_surface_treatment_alt =new HashMap<>();
                        color_surface_treatment_alt.put("value",Translator.translate("zh-CN", "en", product.getColor_surface_treatment()));
                        dataPs.put("color_surface_treatment_alt",color_surface_treatment_alt);
                    }
                    if (!product.getProduct_description().equals("")){
                        Map<String,Object> product_description_alt =new HashMap<>();
                        product_description_alt.put("value",Translator.translate("zh-CN", "en", product.getProduct_description()));
                        dataPs.put("product_description_alt",product_description_alt);
                    }
                    if (!product.getProduct_remarks().equals("")){
                        Map<String,Object> product_remarks_alt =new HashMap<>();
                        product_remarks_alt.put("value",Translator.translate("zh-CN", "en", product.getProduct_remarks()));
                        dataPs.put("product_remarks_alt",product_remarks_alt);
                    }
                    if (!product.getAccessory().equals("")){
                        Map<String,Object> accessory_alt =new HashMap<>();
                        accessory_alt.put("value",Translator.translate("zh-CN", "en", product.getAccessory()));
                        dataPs.put("accessory_alt",accessory_alt);
                    }

                    parameterInster.put("data",dataPs);
                    String s = JSONUtil.toJsonStr(parameterInster);
                    JSONObject entries = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, s);
                    log.info("全系列产品报价"+entries);
                }
            } catch (Exception e) {
                throw new RuntimeException("翻译字段出错！！！"+e);
            }
        }while (fale);
    }
}
